<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class AuthRule extends Model
{
    /**
     * Use Thinkphp ORM Model
     */

    protected $autoWriteTimestamp = 'int';

    /**
     * 获取树形栏目目前只支持到两级
     * @params string
     * @return
     */
    public static function treeMenu()
    {
        $first = Db::name('auth_rule')->where('pid', 0)->where('status',1)->where('is_show', 1)->field('id,pid,title,icon,route')
            ->order(['weigh'=>'asc', 'id'=>'desc'])
            ->select()->toArray();
        if(!empty($first))
        {
            foreach ($first as $k =>$v)
            {
                $first[$k]['second'] = Db::name('auth_rule')->where('status',1)->where('pid', $v['id'])->where('is_show', 1)
                    ->field('id,pid,title,icon,route')
                    ->order(['weigh'=>'asc', 'id'=>'desc'])
                    ->select();
            }
        }
        return $first;
    }

    /**
     * 获取栏目所有和权限验证
     * @params string
     * @return
     */
    public static function fullTreeMenu()
    {
        $first = Db::name('auth_rule')->where('pid', 0)->field('id,pid,title,icon')
            ->order(['weigh'=>'asc', 'id'=>'asc'])
            ->select()->toArray();
        if(!empty($first))
        {
            foreach ($first as $k =>$v)
            {
                $first[$k]['second'] = Db::name('auth_rule')->where('pid', $v['id'])
                    ->field('id,pid,title,icon')
                    ->order(['weigh'=>'asc', 'id'=>'asc'])
                    ->select()->toArray();
                foreach ($first[$k]['second'] as $k1 =>$v1)
                {
                    $first[$k]['second'][$k1]['auth'] = Db::name('auth_rule')->where('pid', $v1['id'])
                        ->field('id,pid,title,icon')
                        ->order(['weigh'=>'asc', 'id'=>'asc'])
                        ->select();
                }
            }
        }
        return $first;
    }

    /**
     * 获取某个角色用户的栏目权限
     * @params string
     * @return
     */
    public static function getRoleAuthRule($role_id)
    {
        $group_info = AuthGroup::where('id', $role_id)->where('status',1)->value('rules');
        if($group_info == '*')
        {
            return self::treeMenu();
        }
        $menu = self::whereIn('id', $group_info)->where('pid',0)->where('status',1)->where('is_show', 1)
            ->field('id,pid,title,icon,route')
            ->order(['weigh'=>'asc', 'id'=>'desc'])
            ->select()->toArray();

        if(!empty($menu))
        {
            foreach ($menu as $k =>$v)
            {
                $menu[$k]['second'] = Db::name('auth_rule')->where('pid', $v['id'])->whereIn('id', $group_info)
                    ->where('status',1)->where('is_show', 1)
                    ->field('id,pid,title,icon,route')
                    ->order(['weigh'=>'asc', 'id'=>'desc'])
                    ->select();
            }
        }

        return $menu;
    }

    /**
     * 获取栏目pid
     * @params string
     * @return
     */
    public static function getPid($id)
    {
        return self::where('id', $id)->value('pid');
    }
}